# 分析接口
# 第一页： 'https://hr.tencent.com/position.php?lid=&tid=&keywords=python&start=0#a'
# 第二页： 'https://hr.tencent.com/position.php?lid=&tid=&keywords=python&start=10#a'
# 第三页： 'https://hr.tencent.com/position.php?lid=&tid=&keywords=python&start=20#a'
# 第n页：start = (n-1)*10

import requests
from lxml import etree


# 定义一个函数，用来判断空
def getDataFormList(temp_list):
    if len(temp_list) > 0:
        return temp_list[0]
    else:
        return ''


page = 1
# for page in range(1, 59, 1)
while True:
    # 生成url
    url = 'https://hr.tencent.com/position.php?lid=&tid=&keywords=python&start=' + str((page - 1) * 10)

    print('第%s页网址' % (page), url)
    # 由于服务器可能使用了反爬虫策略，所以我们应该将自己伪装成一个浏览器
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'
    }

    # 网络访问
    response = requests.get(url, headers=headers)
    # with open('python.html', 'w', encoding='utf-8') as fp:
    #     fp.write(response.text)

    # 数据提取
    tree = etree.HTML(response.text)
    tr_list = tree.xpath('//table[@class="tablelist"]/tr')
    # 由于存在表头和表尾，所以去掉表头和表尾的tr标签节点
    tr_list.pop(0)  # 删除表头
    tr_list.pop()  # 删除表尾
    # 剩下的是10个职位数据信息
    if len(tr_list) == 0:
        print('超出页码范围')
        break

    for tr in tr_list:
        td_list = tr.xpath('./td')

        # 需要职位名称，类别，人数，地点，发布时间
        # 名称
        job_name_list = td_list[0].xpath('./a/text()')
        job_name = getDataFormList(job_name_list)
        # print(job_name)
        # 类别
        job_type_list = td_list[1].xpath('./text()')
        job_type = getDataFormList(job_type_list)
        # 人数
        job_number_list = td_list[2].xpath('./text()')
        job_number = getDataFormList(job_number_list)
        # 地点
        job_addr_list = td_list[3].xpath('./text()')
        job_addr = getDataFormList(job_addr_list)
        # 发布时间
        job_time_list = td_list[4].xpath('./text()')
        job_time = getDataFormList(job_time_list)

        print('第%s页数据' % (page), job_name, job_type, job_number, job_addr, job_time)
    page += 1
    # print(tr_list)
    # print(len(tr_list))